//
//  MFYAnimationManager.m
//  HandyService
//
//  Created by jjkj on 2017.7.11.
//  Copyright © 2017年 jjkj. All rights reserved.
//
/* ***********************************************************************
 * 项目名称:
 * 项目组:
 * 创建时间:2017.7.11
 * 创建人:马方印
 * 用途:自定义modal动画
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/
#import "MFYAnimationManager.h"
#import "MFYPresentationController.h"

@interface MFYAnimationManager(){
    
    BOOL _isPresent;//判断是present 还是 dismiss
}

@end

@implementation MFYAnimationManager

+ (MFYAnimationManager *)shareMFYAnimationManater{
    
    static dispatch_once_t onceToken;
    
    static MFYAnimationManager *manager = nil;
    
    dispatch_once(&onceToken, ^{
        
       manager = [[MFYAnimationManager alloc]init];
    });
    
    return manager;
}

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    MFYPresentationController *presentationController = [[MFYPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    presentationController.presentedViewFrame = self.presentedViewFrame;
    return presentationController;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    _isPresent = YES;
    
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    _isPresent = NO;
    
    return self;
}

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    return self.animationTime;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    if (_isPresent) {
     
        UIView *containerView = [transitionContext containerView];
        UIView *presentedView = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
        presentedView.frame = containerView.bounds;
        [containerView addSubview:presentedView];
        
        CGAffineTransform transform = presentedView.transform;
        presentedView.transform = CGAffineTransformScale(transform, 1.0, 0.0);
        presentedView.layer.anchorPoint = CGPointMake(0.5, 0.0);
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            
            presentedView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
            [transitionContext completeTransition:YES];
        }];
    }
    else{
        
        UIView *dismissView = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
        CGAffineTransform transform = dismissView.transform;
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
           
            dismissView.transform = CGAffineTransformScale(transform, 1.0, 0.00001);
            
        } completion:^(BOOL finished) {
            
            [transitionContext completeTransition:YES];
        }];
    }
}

@end
